package com.diudiutechnology.www.yadiudiu.util.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by Administrator on 2017/1/7 0007.
 */
public class PermissionsChecker {

//主要权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    //次要权限
    static final String[] PERMISSIONSCI = new String[]{
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.READ_CONTACTS
    };


    /**
     * Context context 当前Activity
     * set  0、主要权限 :resultCode -1、所有   0、相机   1、定位  2、储存
     *1、次要权限:resultCode  0、短信   1、通讯录
     * */
    public static boolean getPermissionsChecker(Context context,String set,int resultCode){
        if (set.equals("0")){
            return getPermissionszui (context,resultCode);
        }else if (set.equals("1")){
            return getPermissionsci (context,resultCode);
        }
        return false;
    }
   public static boolean getPermissionszui (Context context,int resultCode){
        if (resultCode<0){
            getPermissions(context);
            return true;
        }else {
         return startPermissionszu(context,resultCode);
        }
    }
    public static boolean getPermissionsci (Context context,int resultCode){
       return startPermissionsci(context,resultCode);
    }
   public static void getPermissions(Context context){
        if (lacksPermissions(context,PERMISSIONS)) {
            startPermissions(context);
        }
   }
    public static void startPermissions(Context context) {
        ActivityCompat.requestPermissions((Activity) context, PERMISSIONS, 1);
    }

    public static boolean startPermissionszu(Context context,int resultCode) {
        if (ContextCompat.checkSelfPermission(context, PERMISSIONS[resultCode]) == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions((Activity) context, new String[]{PERMISSIONS[resultCode]}, 1);
            return true;
        }else {
            return false;
        }

    }

    public static boolean startPermissionsci(Context context,int resultCode) {
        if (ContextCompat.checkSelfPermission(context, PERMISSIONSCI[resultCode]) == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions((Activity) context, new String[]{PERMISSIONSCI[resultCode]}, 1);
            return true;
        }else {
            return false;
        }

    }
        // 判断权限集合
        public static boolean lacksPermissions(Context context,String... permissions) {
            for (String permission : permissions) {
                if (lacksPermission(context,permission)) {
                    return true;
                }
            }
            return false;
        }

        // 判断是否缺少权限
        private static boolean lacksPermission(Context context,String permission) {
            return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
        }
}


